package com.tree.rpc.socket.consumer;

import com.tree.rpc.socket.service.ServiceDispatcher;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @ClassName com.tree.rpc.socket.consumer
 * Description: <类功能描述>. <br>
 * <p>
 * BIO调用远程Server
 * </p>
 * @Author tree
 * @Date 2019-11-07 00:39
 * @Version 1.0
 */
public class BioSocketClient {
    public Object callRemoteServer(Object reqObj,String host,int port) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        Object respObj = null;
        try {
            Socket socket = new Socket(host, port);

            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(reqObj);
            oos.flush();

            ois = new ObjectInputStream(socket.getInputStream());
            respObj = ois.readObject();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return respObj;
        }
    }
}
